<?php
class ActionsController extends AppController{
    public $name = 'Actions';
    private $seperator = '__';
    private $callbacks = array('beforeFilter', 'beforeRender', 'afterFilter');

    // Add and remove controller actions for Aco
    public function index(){
        if(!empty($this->data)){
            $this->__processActions();
        }
        $this->__listActions();
    }

    public function security(){}

    // Add or delete actions
    private function __processActions(){
        $seperator = $this->seperator;
        $securityAccess = $this->data['Actions']['SecurityAccess'];
        //debug($securityAccess);
        $aco =& $this->Acl->Aco;
        foreach($securityAccess as $name_pair_key => $access_selection){
            $name_pair = explode($seperator, $name_pair_key);
            $controller = Inflector::singularize($name_pair[0]);
            $action = $name_pair[1];
            if($access_selection == 'delete'){
                $aco_record = $aco->find(array('Aco.model' => $controller,'Aco.alias' => $action));
                if(!empty($aco_record)){
                    $delete_id = $aco_record['Aco']['id'];
                    $this->log('delete ACO record $delete_id');
                    $aco->delete($delete_id);
                }
            }elseif($access_selection == 'include'){
                $parent_id = '0';
                // Find the parent, if no parent, we create one
                $aco_parent_record = $aco->find(array(
                    'Aco.model' => $controller,
                    'Aco.alias' => $name_pair[0]));
                if(empty($aco_parent_record)){
                    $this->log('save ACO Parent record with data: ');
                    $this->log(array(
                        'model' => $controller,
                        'foreign_key' => '',
                        'alias' => $name_pair[0],
                        'parent_id' => ''
                      ));
                    $aco->create();
                    $aco->save(array(
                        'model' => $controller,
                        'foreign_key' => '',
                        'alias' => $name_pair[0],
                        'parent_id' => ''
                      ));
                    $parent_id = $aco->id;
                }else{
                    $parent_id =  $aco_parent_record['Aco']['id'];
                }
                // now lets create the aco record itself
                $this->log('save ACO record with data: ');
                $this->log(array(
                    'model' => $controller,
                    'foreign_key' => '',
                    'alias' => $action,
                    'parent_id' => $parent_id
                  ));
                $aco->create();
                $aco->save(array(
                    'model' => $controller,
                    'foreign_key' => '',
                    'alias' => $action,
                    'parent_id' => $parent_id
                  ));
            }
        }
    }

    // get all the actions in the controllers
    private function __listActions(){
        $actions = array();
        $seperator = $this->seperator;
        App::import('File', 'Folder');
        $folder = new Folder(APP.'controllers/');
        $folders = $folder->find();
        foreach($folders as $file){
            if(is_file(APP.'controllers/'.$file)){
                $file = new File(APP.'controllers/'.$file);
                $file_contents = $file->read();
                $file->close();
                // get the controller name
                $class_pattern = '/class [a-zA-Z0-9]*Controller extends AppController/';
                preg_match($class_pattern, $file_contents, $matches);
                $class_name_1 = str_replace('class ', '', $matches[0]);
                $class_name = str_replace('Controller extends AppController', '', $class_name_1);
                // get the action names
                $pattern = '/public function [a-zA-Z0-9]*\(/';
                preg_match_all($pattern, $file_contents, $matches);
                // handle all actions to good format
                if(count($matches[0])){
                    // now gather action details together
                    $action_group = array();
                    $class_name_sing = Inflector::singularize($class_name);
                    $public_actions = array();
                    foreach($matches[0] as $i=>$action){
                        $action = trim(str_replace(array('public function', '('), '', $action));
                        if(!in_array($action, $this->callbacks)){
                            $public_actions[] = array(
                                'action' =>$action,
                                'model_alias' => $class_name . $seperator . $action,
                                'model' => $class_name_sing . $seperator . $action);
                        }
                    }
                    $action_group[ 'name' ] = $class_name;
                    $action_group[ 'name_singular' ] = $class_name_sing;
                    $action_group[ 'actions' ] = $public_actions;
                    $actions[] = $action_group;
                }else{
                    continue;
                }
            }
        }
        $this->set('actions', $actions);
        // Get the full list of Aco records
        $aco =& $this->Acl->Aco;
        $aco_list = $aco->find('all');
        $result = array();
        foreach ($aco_list as $current_aco){
            $key_0 = $current_aco['Aco']['model'];
            $key_1 = $current_aco['Aco']['alias'];
            $result[ $key_0.$seperator.$key_1 ] = $current_aco;
        }
        unset($aco_list);
        $this->set('aco_list', $result);
    }
}
?>